import { getLogin } from '@/api/login'
import { getToken, getUserInfo, setToken, setUserInfo } from '@/utils/storage'

const state = {
    token: getToken() || '',
    userInfo: JSON.parse(getUserInfo()),
}

const mutations = {
    setToken(state, payload) {
        state.token = payload
        setToken(payload)
    },
    setUserInfo(state, payload) {
        state.userInfo = payload
        setUserInfo(JSON.stringify(payload))
    }
}

const actions = {
    async getLogin(context, payload) {
        const res = await getLogin(payload)
        const success = res.data.code === 200
        if (success) {
            context.commit('setToken', res.data.data.token.token)
            context.commit('setUserInfo', res.data.data.user)
        }
        console.log(res)
        return success
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
